//
//  MessageViewController.m
//  Doctors
//
//  Created by 王森 on 16/4/13.
//  Copyright © 2016年 王森. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageTableViewCell.h"
#import "MJRefresh.h"
#import "MessageModel.h"
#import "NSString+Size.h"
#import "MessageDetailViewController.h"
@interface MessageViewController ()
{
    int page;
//    NSString *timeString;
//    NSString *titleString;
//
//    NSString *contentString;

}
@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    page=1;
    
    [self setTextTitleViewWithFrame:CGRectMake(0, 0, 60, 20) title:@"通知消息" fontSize:18];
    [self customTableView];
    //    self.tableview.frame=CGRectMake(0, slideControl.bottom, SCREEN_WIDTH, ScreenHeight-64-48-48);
    //
    
    [self addLeftBackBtn:nil];
    
    [self requestData];
    
    UINib *nib=[UIView loadNibNamed:@"MessageTableViewCell" bundle:nil];
    __unsafe_unretained __typeof(self) weakSelf = self;
    self.tableview.mj_footer=[MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        page++;
        [self requestData];
        
    }];

    self.tableview.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.dataArray removeAllObjects];
        page=1;
        [weakSelf requestData];
        
           }];

    [self.tableview registerNib:nib forCellReuseIdentifier:@"MessageTableViewCell"];
      // Do any additional setup after loading the view.

    // Do any additional setup after loading the view.
}

-(void)requestData
{
    
    
    [self addProgressHUD];
    
    NSDictionary *dictionary=[NSDictionary dictionaryWithObjectsAndKeys: [self getArtisanId],@"artisanId",[NSString stringWithFormat:@"%d",page],@"page",@"10",@"size",  nil];
    NSLog(@"订单详情参数：%@",dictionary);
    [[WSNetworkApi shareManager]postWithUrl:@"/notice/queryNoticeList" parameters:dictionary option:RequestCenterCachePolicyNormal sucess:^(id responseObject) {
        [self.tableview.mj_header endRefreshing];
        
        [self.tableview.mj_footer endRefreshing];
        

        [self removeStatuslable];
        
        NSLog(@"%@",responseObject);
        
        if ([responseObject[@"status"] isEqualToString:@"ok"]) {
            
            NSArray *array=responseObject[@"noticeList"];
            for (NSDictionary *dic in array) {
                MessageModel *model=[[MessageModel alloc]initWithDic:dic];
                [self.dataArray addObject:model];
                
                
            }
            
            
        }
        else{
            
            [self showFailed:responseObject[@"message"]];
            
        }
        
        [self.tableview reloadData];
        
        
    } failur:^(NSError *error) {
        
        [self removeStatuslable];
        [self.tableview.mj_header endRefreshing];
        
        [self.tableview.mj_footer endRefreshing];
        

    }];
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //    __weak typeof(self)weakSelf=self;
    //
        MessageModel *model=self.dataArray[indexPath.row];
    
    MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageTableViewCell" forIndexPath:indexPath];
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    NSString *subTItle=[NSString quDiaoTeShuZiFu:model.content];
    NSString *title=[NSString quDiaoTeShuZiFu:model.title];

    cell.title.text=title;
    
    cell.subtitle.text=subTItle;
    cell.time.text=[NSString getDateWithTimeString:model.time];
    
    
    
//    [cell cellFuzhi:self.dataArray[indexPath.row]];
    
    
    return cell;
    
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 64;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MessageModel *model=self.dataArray[indexPath.row];

    ALLOC(MessageDetailViewController, view);
    NSString *subTItle=[NSString quDiaoTeShuZiFu:model.content];
    NSString *title=[NSString quDiaoTeShuZiFu:model.title];
    

    view.titleSting=title;
    view.contentSting=subTItle;
    view.timeSting=[NSString getDateWithTimeString:model.time];
    view.hidesBottomBarWhenPushed=YES;
    
    [self pushNextViewController:view];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
